const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ProductInStoreId: 0,
    proList: [],
    ProviderId: 0,
    ProviderName: "",
    HouseId: 0,
    HouseName: '',
    Remark: '',
    freight: '',
    showTopTips: false,
    errorText: ''
  },
  changeText: function (e) {
    let name = e.currentTarget.dataset.name;
    if ((typeof e.currentTarget.dataset.index) == 'number'){
      let i = e.currentTarget.dataset.index;
      let arr = this.data.proList
      arr[i][name] = e.detail && e.detail.value
      this.setData({
        proList: arr
      })
    }else{
      let nameMap = {}
      nameMap[name] = e.detail && e.detail.value
      this.setData(nameMap)
    }
    
  },
  changeNum: function (e) {
    let name = e.currentTarget.dataset.name;
    let txt = e.detail && e.detail.value.replace(/[^\d.-]/g, "")
    if ((typeof e.currentTarget.dataset.index) == 'number') {
      let i = e.currentTarget.dataset.index;
      let arr = this.data.proList
      arr[i][name] = txt
      this.setData({
        proList: arr
      })
    }else{
      let nameMap = {}
      nameMap[name] = txt
      this.setData(nameMap)
    }
  },
  //左移删除
  touchstart: function (e) {
    this.setData({
      index: e.currentTarget.dataset.index,
      Mstart: e.changedTouches[0].pageX
    });
  },
  touchmove: function (e) {
    //列表项数组
    let list = this.data.proList;
    //手指在屏幕上移动的距离
    //移动距离 = 触摸的位置 - 移动后的触摸位置
    let move = this.data.Mstart - e.changedTouches[0].pageX;
    // 这里就使用到我们之前记录的索引index
    //比如你滑动第一个列表项index就是0，第二个列表项就是1，···
    //通过index我们就可以很准确的获得当前触发的元素，当然我们需要在事前为数组list的每一项元素添加x属性
    list[this.data.index].x = move > 0 ? -move : 0;
    this.setData({
      proList: list
    });
  },
  touchend: function (e) {
    let list = this.data.proList;
    let move = this.data.Mstart - e.changedTouches[0].pageX;
    list[this.data.index].x = move > 100 ? -180 : 0;
    this.setData({
      proList: list
    });
  },
  delPro: function (e) {
    var i = e.currentTarget.dataset.index
    var arr = this.data.proList
    arr.splice(i, 1)
    this.setData({
      proList: arr
    })
  },
  compReq: function () {
    var that = this
    var stop = true
    if (this.data.proList.length>0){
      for (let i = 0; i < this.data.proList.length; i++) {
        if (this.data.proList[i].Quantity && this.data.proList[i].Quantity != '' && this.data.proList[i].Quantity != 0) {
        } else {
          this.setData({
            showTopTips: true,
            errorText: '请输入正确的数量'
          })
          setTimeout(function () {
            that.setData({
              showTopTips: false,
              errorText: ''
            })
          }, 2000)
          stop = false
          return false
        }
        if (this.data.proList[i].Price && this.data.proList[i].Price != '' && this.data.proList[i].Price != 0) {
        } else {
          this.setData({
            showTopTips: true,
            errorText: '请输入正确的价格'
          })
          setTimeout(function () {
            that.setData({
              showTopTips: false,
              errorText: ''
            })
          }, 2000)
          stop = false
          return false
        }
      }
      app.globalData.offerList = this.data.proList
      if (this.data.freight != "" && (typeof +this.data.freight) == 'number'){
        app.globalData.freight = this.data.freight
      } else {
        this.setData({
          showTopTips: true,
          errorText: '请输入正确的运费'
        })
        setTimeout(function () {
          that.setData({
            showTopTips: false,
            errorText: ''
          })
        }, 2000)
        stop = false
        return false
      }
      if(stop){
        var str = encodeURIComponent(JSON.stringify(this.data.proList))
        wx.navigateTo({
          url: './newOffer/newOffer?list=' + str + '&freight=' + this.data.freight
        })
      }
    }else{
      this.setData({
        showTopTips: true,
        errorText: '至少选择一个产品'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})